//
//  ZZTaskContentView.swift
//  QMXK
//
//  Created by jenkin on 2018/12/12.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskContentView: UIView {

    var dataList:ZZTaskListModel?
    var topLineLayer:CAShapeLayer?
    var lowLineLayer:CAShapeLayer?
    var k:CGFloat?
    var b:CGFloat?
    var layerList:Array<CAShapeLayer> = Array<CAShapeLayer>.init()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.topLineLayer = CAShapeLayer.init()
        self.topLineLayer?.lineWidth = 2
        self.topLineLayer?.strokeColor = UIColor.red.cgColor
        self.lowLineLayer = CAShapeLayer.init()
        self.lowLineLayer?.lineWidth = 2
        self.lowLineLayer?.strokeColor = UIColor.green.cgColor
        self.layer.addSublayer(self.topLineLayer!)
        self.layer.addSublayer(self.lowLineLayer!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func drawLineType(type:Int, indexArray:Array<Any>){
        let beginIndex:Int = indexArray[0] as! Int
        let endIndex:Int = indexArray[1] as! Int
        //画最高点
        if type == 0{
            //self.topLineLayer?.path = self.getPath(type: 0, beginIndex: beginIndex, endIndex: endIndex)
            let topLineLayer:CAShapeLayer = CAShapeLayer.init()
            topLineLayer.lineWidth = 2
            topLineLayer.strokeColor = UIColor.red.cgColor
            topLineLayer.path = self.getPath(type: 0, beginIndex: beginIndex, endIndex: endIndex)
            self.layer.addSublayer(topLineLayer)
            self.layerList.append(topLineLayer)
        }
        //画最地点
        else{
            //self.lowLineLayer?.path = self.getPath(type: 1, beginIndex: beginIndex, endIndex: endIndex)
            let lowLineLayer:CAShapeLayer = CAShapeLayer.init()
            lowLineLayer.lineWidth = 2
            lowLineLayer.strokeColor = UIColor.green.cgColor
            lowLineLayer.path = self.getPath(type: 1, beginIndex: beginIndex, endIndex: endIndex)
            self.layer.addSublayer(lowLineLayer)
            self.layerList.append(lowLineLayer)
        }
    }
    
    func getPath(type:Int, beginIndex:Int, endIndex:Int) ->CGPath{
        let beth:UIBezierPath = UIBezierPath.init()
        let beginModel:ZZTaskModel! = self.dataList?.data[beginIndex]
        let endModel:ZZTaskModel! = self.dataList?.data[endIndex]
        
        let rect:CGRect = self.bounds
        let vh:CGFloat = rect.size.height
        let dish:CGFloat! = self.dataList!.allMax - self.dataList!.allMin
        let rate:CGFloat = vh / dish
        let cellW:CGFloat = CommenConst.lineCellW
        let beginX:CGFloat = cellW / 2 + cellW * CGFloat(beginIndex)
        let endX:CGFloat = cellW / 2 + cellW * CGFloat(endIndex)
        
        var beginY:CGFloat = vh - (beginModel.max - self.dataList!.allMin) * rate
        var endY:CGFloat = vh - (endModel.max - self.dataList!.allMin) * rate
        if type == 1{
            beginY = vh - (beginModel.min - self.dataList!.allMin) * rate
            endY =  vh - (endModel.min - self.dataList!.allMin) * rate
        }
        
        beth.move(to: CGPoint.init(x: beginX, y: beginY))
        beth.addLine(to: CGPoint.init(x: endX, y: endY))
        

        //多画一条线
        if (beginX - endX) != 0{
            let k:CGFloat = (beginY - endY) / (beginX - endX)
            self.k = k
            let b:CGFloat = beginY - k * beginX
            self.b = b
            let longX:CGFloat = rect.size.width
            let longY:CGFloat = k * longX + b
            beth.addLine(to: CGPoint.init(x: longX, y: longY))
        }
        
        beth.stroke()
        return beth.cgPath
    }
    
    func getLongpressY(x:CGFloat){
        let y:CGFloat = (self.k ?? 0) * x + (self.b ?? 0)
        let dish:CGFloat! = self.dataList!.allMax - self.dataList!.allMin
        let vh:CGFloat = self.bounds.size.height
        let rate:CGFloat = vh / dish
        let price:CGFloat =  (vh - y) / rate + (self.dataList?.allMin ?? 0)
        
        let text:String = "x=\(x),y=\(price)"
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dataShow"), object: nil, userInfo: ["text": text])
    }
    
    //取消上一条线条
    func cancelLastLine(){
        let count:Int = self.layerList.count
        if count == 0{
            return
        }
        
        let layer:CAShapeLayer = self.layerList[count - 1]
        self.layerList.remove(at: count - 1)
        layer.removeFromSuperlayer()
    }
}
